﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;

namespace ShevaEngine.Core.Foundation
{
    /// <summary>
    /// Serializer pro KeyValuePair(int, int)
    /// </summary>
    [ContentTypeSerializer]
    public class KeyValuePairSerializer : ContentTypeSerializer<KeyValuePair<int, int>>
    {
        /// <summary>
        /// Method serialize.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="value"></param>
        /// <param name="format"></param>
        protected override void Serialize(IntermediateWriter output, KeyValuePair<int, int> value, ContentSerializerAttribute format)
        {
            output.Xml.WriteStartElement("Key");
            output.Xml.WriteString(value.Key.ToString());
            output.Xml.WriteEndElement();

            output.Xml.WriteStartElement("Value");
            output.Xml.WriteString(value.Value.ToString());
            output.Xml.WriteEndElement();
        }

        /// <summary>
        /// Method deserialize.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format"></param>
        /// <param name="existingInstance"></param>
        /// <returns></returns>
        protected override KeyValuePair<int, int> Deserialize(IntermediateReader input, ContentSerializerAttribute format, KeyValuePair<int, int> existingInstance)
        {
            try
            {
                string keyStr = input.Xml.ReadElementContentAsString("Key", "");
                string valueStr = input.Xml.ReadElementContentAsString("Value", "");

                return new KeyValuePair<int, int>(int.Parse(keyStr), int.Parse(valueStr));
            }

            catch
            {
                return existingInstance;
            }
        }
    }

    /// <summary>
    /// Serializer pro KeyValuePair(int, int)
    /// </summary>
    [ContentTypeSerializer]
    public class KeyValuePairStringStringSerializer : ContentTypeSerializer<KeyValuePair<string, string>>
    {
        /// <summary>
        /// Method serialize.
        /// </summary>
        /// <param name="output"></param>
        /// <param name="value"></param>
        /// <param name="format"></param>
        protected override void Serialize(IntermediateWriter output, KeyValuePair<string, string> value, ContentSerializerAttribute format)
        {
            output.Xml.WriteStartElement("Key");
            output.Xml.WriteString(value.Key);
            output.Xml.WriteEndElement();

            output.Xml.WriteStartElement("Value");
            output.Xml.WriteString(value.Value);
            output.Xml.WriteEndElement();
        }

        /// <summary>
        /// Method deserialize.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="format"></param>
        /// <param name="existingInstance"></param>
        /// <returns></returns>
        protected override KeyValuePair<string, string> Deserialize(IntermediateReader input, ContentSerializerAttribute format, KeyValuePair<string, string> existingInstance)
        {
            try
            {
                string keyStr = input.Xml.ReadElementContentAsString("Key", "");
                string valueStr = input.Xml.ReadElementContentAsString("Value", "");

                return new KeyValuePair<string, string>(keyStr, valueStr);
            }

            catch
            {
                return existingInstance;
            }
        }
    }
}
